'''
    @project: Masdevallia
    @author: Austin Baker
    @created: 2/22/2014
    @license: http://opensource.org/licenses/mit-license.php

    The MIT License (MIT)

    Copyright (c) 2014 Austin Baker
    
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    
    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.
    
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
    
    ------
    
    About:
'''

import maya.OpenMaya as OpenMaya

import masdevallia.utility.Dag as Dag
import masdevallia.nodes.Node as Node
import masdevallia.utility.Matrix as Matrix
import masdevallia.rigging.modules.RigModuleMarker as RigModuleMarker

class RigModule( object ):
    '''
    Rig module object.
    '''
    def __init__( self ):
        # Module properties.
        self.module_name = None
        self.character_side = None
    
    def createModule( self ):
        '''
        Creates the module
        '''
        pass
    
    def createMarker(self, name, matrix_list, meta_node, plug_name ):
        '''
        Creates a Marker.
        
        @param name: String. Name of the marker.
        @param matrix_list: List of Float. Default matrix of the marker.
        @param meta_node: MetaNode for the module.
        @param plug_name: String. Name of the Meta Node plug the marker should be connected
            to.
        @return: RigModuleMarker.
        '''
        # Create the matrix
        matrix = OpenMaya.MMatrix()
        OpenMaya.MScriptUtil.createMatrixFromList( matrix_list, matrix )
        
        # Create the spacer.
        spacer_name = 'sp_{0}'.format( name )
        new_node = Dag.createNode( spacer_name, 'transform', self.root.asMObject() )
        spacer = Node.Node( new_node )
        
        # Create the marker.
        marker = RigModuleMarker.RigModuleMarker( name=name )
        marker.module_root = self.root.name
        marker.createMarker( parent=spacer.asMObject() )
        
        # Parent the marker to the root.
        spacer.setParent( self.root.asMObject() )
        
        # Apply the matrix.
        spacer_matrix = Matrix.Matrix( spacer_name, 'worldMatrix' )
        spacer_matrix.setMatrix( matrix )
        
        # Connect the marker to the meta node.
        Dag.connectNodes( marker.mobject, 'metaNode', meta_node.mobject, plug_name )
        
        # Return the Marker.
        return marker
    
    def getEditorUi( self, layout ):
        '''
        Creates the modules editor UIs and adds them to the passed in
        UI.
        
        @param layout: QLayout.
        '''
        pass